package com.lqd.mall.system.controller;

import com.lqd.mall.basis.response.CommonResult;
import com.lqd.mall.basis.response.Result;
import com.lqd.mall.basis.utils.poi.ExcelUtil;
import com.lqd.mall.interaction.basis.vo.TableDataInfoVo;
import com.lqd.mall.interaction.system.SysDictDataEntity;
import com.lqd.mall.jpa.controller.BaseController;
import com.lqd.mall.log.annotation.Log;
import com.lqd.mall.log.enums.BusinessType;
import com.lqd.mall.system.SysDictDataControllerApi;
import com.lqd.mall.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 零起点
 * @create 2022-03-20 21:58
 */
@RestController
public class SysDictDataController
        extends BaseController implements SysDictDataControllerApi {

    @Autowired
    ISysDictDataService sysDictDataService;

    @Override
    public Result<List<SysDictDataEntity>> findSysDictData(String dictType) {
        List<SysDictDataEntity> entities = sysDictDataService.findSysDictData(dictType);
        return Result.SUCCESS(entities);
    }

    @Override
    public Result<TableDataInfoVo<SysDictDataEntity>> list(SysDictDataEntity entity) {
        TableDataInfoVo<SysDictDataEntity> infoVo = sysDictDataService.list(entity);
        return Result.SUCCESS(infoVo);
    }

    @Override
    public Result<SysDictDataEntity> findSysDictDataById(Long dictCode) {
        return Result.SUCCESS(sysDictDataService.selectById(dictCode));
    }

    @Override
    @Log(title = "修改字典值数据", businessType = BusinessType.UPDATE)
    public CommonResult updateSysDictDataById(SysDictDataEntity entity) {
        return result(sysDictDataService.updateEntity(entity, "dict_code").get());
    }

    @Override
    public CommonResult saveSysDictDataById(SysDictDataEntity entity) {
        return result(sysDictDataService.save(entity).get());
    }

    @Override
    @Log(title = "字典数据值导出", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, SysDictDataEntity entity) {
        TableDataInfoVo<SysDictDataEntity> infoVo = sysDictDataService.list(entity);
        List<SysDictDataEntity> infoVoRows = infoVo.getRows();
        ExcelUtil<SysDictDataEntity> excelUtil = new ExcelUtil<SysDictDataEntity>(SysDictDataEntity.class);
        excelUtil.exportExcel(response, infoVoRows, "字典数据集合", "字典数据");
    }

    @Override
    @Log(title = "删除字典值", businessType = BusinessType.DELETE)
    public CommonResult deleteSysDictData(Long dictCode) {
        sysDictDataService.deleteSysDictData(dictCode);
        return null;
    }

}
